# StartTCL: n
######################
#   AntiPvp System   #
######################
#
#
#-General Commands:
#.antipvp info		                    			-Returns jailtime, max level difference, non-PvP places and antipvp stance [on/off].
#
#-Plevel 4+ Commands: 
#.antipvp on/off			    			-Enable/Disable Anti-PvP.
#.antipvp jail on/off		            			-Enable/Disable jail for Anti-PvP, warning remains.
#.antipvp leveldiff <level>	            			-Change maximal level difference between killer and victim. Resets after .retcl or restart.
#.antipvp jailtime <seconds>	            			-Change jailtime. Resets after .retcl or server restart.
#
#-Plevel 6+ Commands:
#.antipvp resetzone		            			-Deletes the last zone added from the list.
#.antipvp resetarea		            			-Deletes the last "square" added from the list.
#.antipvp addzone <zoneid>		    			-Adds zone to Anti-PvP zone check.
#.antipvp namearea <name of the area>	    			-Adds name to newest "square" from "Square Check. 
#.antipvp resetnamearea			        		-Resets the last name added to "square".
#.antipvp addarea <map> <bigX> <smallX> <bigY> <smallY> 	-Adds place between 4 coords to "Square Check".

	variable mapid
	variable zoneid
	variable atc
	variable ztc
	variable xtop
	variable yleft
	variable yright
	variable xbottom
	variable saytext
	variable PvPJail 1
	variable PvPActive 1
	variable LoadCoords 1
	variable maxleveldiff 10
	variable PvPJailTime 1800

namespace eval nAntiPvP {

proc AntiPvP { player cargs } {
    	global PvPJail
    	global saytext
    	global PvPActive
    	global LoadCoords
    	global maxleveldiff
    	global PvPJailTime
     	if { [ lindex $cargs 0 ] == "info" } {
	if { $PvPActive == 1 } {
	set PvPreturn ON
	} else {
	set PvPreturn OFF
	}
	if { $LoadCoords == 1 } {
	set rhandle [open "logs/AntiPvP/namearea.txt" ]
	gets $rhandle saytext
	close $rhandle
	}
	set JailMins [ expr ($PvPJailTime /60)]
	return "You will receive one warning! After this you will be jailed!\nJailtime: $JailMins minutes\nAllowed level difference: +/-$maxleveldiff \nPvP restricted in: $saytext.\nAntiPvP is $PvPreturn!"
     	}
     	if { [GetPlevel $player] >= 4 } {
	switch [ lindex $cargs 0 ] {
	"off" {
	set PvPActive 0
	return "Anti-PvP is now deactivated!"
	}
	"on" {
	set PvPActive 1
	return "Anti-PvP is now activated!"
     	}
	"jail" {
	switch [ lindex $cargs 1 ] {
	"off" {
        set PvPJail 0
	return "Anti-PvP jailing is now disabled!"
	}
	"on" {
        set PvPJail 1
	return "Anti-PvP jailing is now enabled!"
	}
	}
	}
	"jailtime" {
    	set PvPJailTime [ lindex $cargs 1 ]
	set JailMins [ expr ($PvPJailTime /60)]
	return "Anti-PvP jailtime is changed to $JailMins!"
	}
	"leveldiff" {
	set maxleveldiff [ lindex $cargs 1 ]
	return "Anti-PvP max. level difference is now +/-$maxleveldiff!"
	}
 	"" {
	return "Commands: \n.antipvp info\n.antipvp on/off\n.antipvp jail on/off\n.antipvp leveldiff <level>\n.antipvp jailtime <seconds>\n\nPlevel 6:\n.antipvp resetzone\n.antipvp resetarea\n.antipvp addzone <zoneid>\n.antipvp namearea <''name of the area''>\n.antipvp resetnamearea\n.antipvp addarea <map> <big x coord> <small x coord> <big y coord> <small y coord>"
	}
	}
     	}
     	if { [GetPlevel $player] >= 6 } {
	switch [ lindex $cargs 0 ] {
	"namearea" {
    	set LoadCoords 1
	set Characters [string length $cargs]
	set NameArea [string range $cargs 9 $Characters]
    	set handle [open "logs/AntiPvP/namearea.txt" ]
	gets $handle line
	close $handle
    	set handle [open "logs/AntiPvP/BackUp/namearea.bak" w+]
	puts $handle $line
	close $handle
	set handle [open "logs/AntiPvP/namearea.txt" w+]
	puts $handle "$line$NameArea "
	close $handle
    	return "PvP is forbidden in $NameArea!"
	}
	"resetnamearea" {
	set LoadCoords 1
	set handle [open "logs/AntiPvP/BackUp/namearea.bak" ]
	gets $handle line
	close $handle
	set handle [open "logs/AntiPvP/namearea.txt" w+]
	puts $handle $line
	close $handle
	return "Last added name for area deleted!"
	}
	"addzone" {
   	set LoadCoords 1
	set zone [ lindex $cargs 1 ]
	set handle [open "logs/AntiPvP/numberofzones.txt" ]
	gets $handle line
	close $handle
   	set handle [open "logs/AntiPvP/BackUp/numberofzones.bak" w+]
	puts $handle $line
	close $handle
   	set handle [open "logs/AntiPvP/numberofzones.txt" w+]
	puts $handle [ expr ($line + 1) ]
	close $handle
   	set handle [open "logs/AntiPvP/zone.txt" ]
	gets $handle line
	close $handle
	set handle [open "logs/AntiPvP/BackUp/zone.bak" w+]
	puts $handle $line
	close $handle
	set handle [open "logs/AntiPvP/zone.txt" w+]
	puts $handle "$line$zone "
	close $handle
	return "You added Zone: <[ lindex $cargs 1 ]>"
	}
	"resetzone" {
	set LoadCoords 1
     	set handle [open "logs/AntiPvP/BackUp/numberofzones.bak" ]
	gets $handle line
	close $handle
	set handle [open "logs/AntiPvP/numberofzones.txt" w+]
	puts $handle $line
	close $handle
	set handle [open "logs/AntiPvP/BackUp/zone.bak" ]
	gets $handle line
	close $handle
	set handle [open "logs/AntiPvP/zone.txt" w+]
	puts $handle $line
	close $handle
	return "Last added zone deleted!"
	}
	"addarea" {
	set LoadCoords 1
	set Map [ lindex $cargs 1 ]
	set XBig [ lindex $cargs 2 ]
	set XSmall [ lindex $cargs 3 ]
	set YBig [ lindex $cargs 4 ]
	set YSmall [ lindex $cargs 5 ]
	set handle [open "logs/AntiPvP/numberofareas.txt" ]
	gets $handle line
	close $handle
	set handle [open "logs/AntiPvP/BackUp/numberofareas.bak" w+]
	puts $handle $line
	close $handle
	set handle [open "logs/AntiPvP/numberofareas.txt" w+]
	puts $handle [ expr ($line + 1) ]
	close $handle
	set handle [open "logs/AntiPvP/map.txt" ]
	gets $handle line
	close $handle
	set handle [open "logs/AntiPvP/BackUp/map.bak" w+]
	puts $handle $line
	close $handle
	set handle [open "logs/AntiPvP/map.txt" w+]
	puts $handle "$line$Map "
	close $handle
	set handle [open "logs/AntiPvP/xcoordsbig.txt" ]
	gets $handle line
	close $handle
	set handle [open "logs/AntiPvP/BackUp/xcoordsbig.bak" w+]
	puts $handle $line
	close $handle
	set handle [open "logs/AntiPvP/xcoordsbig.txt" w+]
	puts $handle "$line$XBig "
	close $handle
	set handle [open "logs/AntiPvP/xcoordssmall.txt" ]
	gets $handle line
	close $handle
	set handle [open "logs/AntiPvP/BackUp/xcoordssmall.bak" w+]
	puts $handle $line
	close $handle
	set handle [open "logs/AntiPvP/xcoordssmall.txt" w+]
	puts $handle "$line$XSmall "
	close $handle
   	set handle [open "logs/AntiPvP/ycoordsbig.txt" ]
	gets $handle line
	close $handle
	set handle [open "logs/AntiPvP/BackUp/ycoordsbig.bak" w+]
	puts $handle $line
	close $handle
	set handle [open "logs/AntiPvP/ycoordsbig.txt" w+]
	puts $handle "$line$YBig "
	close $handle
	set handle [open "logs/AntiPvP/ycoordssmall.txt" ]
	gets $handle line
	close $handle
	set handle [open "logs/AntiPvP/BackUp/ycoordssmall.bak" w+]
	puts $handle $line
	close $handle
	set handle [open "logs/AntiPvP/ycoordssmall.txt" w+]
	puts $handle "$line$YSmall "
	close $handle
	return "You have set the folling coordinates:\n<[ lindex $cargs 1 ]> <[ lindex $cargs 2 ]> <[ lindex $cargs 3 ]> <[ lindex $cargs 4 ]> <[ lindex $cargs 5 ]>\n\nMake sure that you add a name for the area now!"
	}
	"resetarea" {
	set LoadCoords 1
	set rhandle [open "logs/AntiPvP/BackUp/numberofareas.bak" ]
	gets $rhandle line
	close $rhandle
	set handle [open "logs/AntiPvP/numberofareas.txt" w+]
	puts $handle $line
	close $handle
	set rhandle [open "logs/AntiPvP/BackUp/map.bak" ]
	gets $rhandle line
	close $rhandle
	set handle [open "logs/AntiPvP/map.txt" w+]
	puts $handle $line
	close $handle
	set rhandle [open "logs/AntiPvP/BackUp/xcoordsbig.bak" ]
	gets $rhandle line
	close $rhandle
	set handle [open "logs/AntiPvP/xcoordsbig.txt" w+]
	puts $handle $line
	close $handle
	set rhandle [open "logs/AntiPvP/BackUp/xcoordssmall.bak" ]
	gets $rhandle line
	close $rhandle
	set handle [open "logs/AntiPvP/xcoordssmall.txt" w+]
	puts $handle $line
	close $handle
	set rhandle [open "logs/AntiPvP/BackUp/ycoordsbig.bak" ]
	gets $rhandle line
	close $rhandle
	set handle [open "logs/AntiPvP/ycoordsbig.txt" w+]
	puts $handle $line
	close $handle
	set rhandle [open "logs/AntiPvP/BackUp/ycoordssmall.bak" ]
	gets $rhandle line
	close $rhandle
	set handle [open "logs/AntiPvP/ycoordssmall.txt" w+]
	puts $handle $line
	close $handle
	return "Last added area is reset!\nMake sure ot reset the name of the area if needed!"
	}
	}
     	}
	}
}

::Custom::AddCommand "antipvp" "nAntiPvP::AntiPvP"
::Custom::HookProcAfter "::WoWEmu::OnPlayerDeath" { 
   	global PvPJail
   	global PvPActive
   	global LoadCoords
   	set Plevel [GetPlevel $killer]
   	set KillerPlayer [GetObjectType $killer]
     	if { $PvPActive == 1 } {
	if { $KillerPlayer == 4 && $Plevel > 2 } {
	::Custom::ReadOnline
	global mapid
	global zoneid
	global atc
	global ztc
	global xtop
	global yleft
	global yright
	global xbottom
	global saytext
	global maxleveldiff
	global PvPJailTime
	if { $LoadCoords == 1 } {
	set LoadCoords 0
	set rhandle [open "logs/AntiPvP/namearea.txt" ]
	gets $rhandle saytext
	close $rhandle
	set rhandle [open "logs/AntiPvP/numberofzones.txt" ]
	gets $rhandle ztc
	close $rhandle
	set rhandle [open "logs/AntiPvP/zone.txt" ]
	gets $rhandle zoneid
	close $rhandle
	set rhandle [open "logs/AntiPvP/numberofareas.txt" ]
	gets $rhandle atc
	close $rhandle
	set rhandle [open "logs/AntiPvP/map.txt" ]
	gets $rhandle mapid
	close $rhandle
	set rhandle [open "logs/AntiPvP/xcoordsbig.txt" ]
	gets $rhandle xbottom
	close $rhandle
	set rhandle [open "logs/AntiPvP/xcoordssmall.txt" ]
	gets $rhandle xtop
	close $rhandle
	set rhandle [open "logs/AntiPvP/ycoordsbig.txt" ]
	gets $rhandle yright
	close $rhandle
   	set rhandle [open "logs/AntiPvP/ycoordssmall.txt" ]
	gets $rhandle yleft
	close $rhandle
	}
	set CheckDone 0
	set LvLVictim [GetLevel $player]
	set LvLKiller [GetLevel $killer]
	set KillerPos [lindex [GetPos $player]]
	set NameKiller [GetName $killer]
	set NamePlayer [GetName $player]
	set XPlayer [ lindex $KillerPos 1 ]
	set YPlayer [ lindex $KillerPos 2 ]
	set MapPlayer [ lindex $KillerPos 0 ]
	set ZonePlayer [ lindex [::Custom::GetOnlineData $NamePlayer] 5 ]
	for { set ZoneChecked 0 } { $ZoneChecked<$ztc } {incr ZoneChecked} {
	set ZoneIDPlayer [ lindex $zoneid $ZoneChecked ]
	if { $ZoneIDPlayer == $ZonePlayer && $NamePlayer != $NameKiller } {
	set CheckDone 1
	set ZoneChecked $ztc
	Say $killer 0 "|cffff0000$NameKiller killed $NamePlayer in a non-pvp zone!"
	CastSpell $killer $killer 7
	Resurrect $player
	SetQFlag $player IsAlive
	ClearQFlag $player IsDead
	CastSpell $player $player 15007
	}
	}
	if { $CheckDone == 0 } {
     	for { set checked 0 } { $checked<$atc } {incr checked} {
	if { (($LvLKiller - $LvLVictim) > $maxleveldiff || ($LvLKiller - $LvLVictim) < -$maxleveldiff || $XPlayer >= [ lindex $xtop $checked ] && $XPlayer <= [ lindex $xbottom $checked ] && $YPlayer >= [ lindex $yleft $checked ] && $YPlayer <= [ lindex $yright $checked ] && $MapPlayer == [ lindex $mapid $checked ]) && $NamePlayer != $NameKiller && $CheckDone == 0 } {
	if { ($LvLKiller - $LvLVictim) <= $maxleveldiff && ($LvLKiller - $LvLVictim) >= -$maxleveldiff } {
	set SayPlayer [ lindex $saytext $checked ]
	Say $killer 0 "|cffff0000$NameKiller killed a player in $SayPlayer!"
	}
	if { [::GetQFlag $killer IsWarned] == 1 && $PvPJail == 1 } {
	package require SQLdb
	if { ! [ info exists handle ] } {
	set ::SQLdb::handle [ ::SQLdb::openSQLdb ]
	}
	set handle $::SQLdb::handle
	set pos [ ::GetPos $killer ]
	set jaildate [clock seconds]
	if { ! [ ::SQLdb::booleanSQLdb $::ngjail::handle "SELECT name FROM `ngjail` WHERE (`name` = '$NameKiller')" ] } {
	::SQLdb::querySQLdb $::ngjail::handle "INSERT INTO `ngjail` (`name`,`PlayerUID`, `jaildate`, `jailtime`, `pos`, `reason`, `jail`, `by` ) VALUES('$NameKiller','$killer', '$jaildate', '$PvPJailTime', '[ ::SQLdb::canonizeSQLdb $pos ]', 'disrespecting the rules', 1 ,'anti-pvp')"
	set totaltimeadd 0
	} else {
	set exijailtime [::SQLdb::firstcellSQLdb $handle "SELECT jailtime FROM `ngjail` WHERE (`name` = '$NameKiller')"]
	set totaltimeadd [ expr ($exijailtime + $PvPJailTime)]
	::SQLdb::querySQLdb $::ngjail::handle "UPDATE `ngjail` SET `PlayerUID`= '$killer', `jaildate` = '$jaildate', `jailtime` = '$totaltimeadd', `pos` = '[ ::SQLdb::canonizeSQLdb $pos ]', `reason` = 'disrespecting the rules', `jail` = 1, `by` = 'anti-pvp' WHERE (`name` = '$NameKiller')"
	}
	set jailmins [ expr ($totaltimeadd /60)]
	Say $killer 0 "I am jailed for $jailmins minutes because i discrespected the rules! (PvP)"
	Teleport $killer 13 248.952652 228.112503 -1131.864624
	}
	set checked $atc
	CastSpell $killer $killer 7
	Resurrect $player
	SetQFlag $player IsAlive
	ClearQFlag $player IsDead
	CastSpell $player $player 15007
	if { [::GetQFlag $killer IsWarned] == 0 } {
	if { ($LvLKiller - $LvLVictim) > $maxleveldiff } {
	Say $killer 0 "|cffff0000$NameKiller killed a Low-Level player!"
	}
	if { ($LvLKiller - $LvLVictim) < -$maxleveldiff } {
	Say $killer 0 "|cffff0000$NameKiller killed a High-Level player!"
	}
	Say $killer 0 "|cffff0000$NameKiller you are warned now! Next time you'll be jailed and you won't get warned again!"
	SetQFlag $killer IsWarned
	}
	}
	}
	}
	}
     	}
}